<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class content_images_model extends CI_Model {
	
    public $config = array(
        'gallery'=>array(
            'source_image' => '', //get original image
            'new_image' => './uploads/images/data/content/', //save as new image //need to create thumbs first
            'maintain_ratio' => false,
            'width' => 270,
            'height' => 200,
            'image_library'=>'gd2',
            'y_axis' => 0,
            'x_axis' => 0,
        ),
        'img'=>array(
            'upload_path' => './uploads/images/data/',
            'allowed_types' => 'gif|jpg|png|jpeg',
            'max_size' => 1000,
            'max_width' => 1912,
            'max_height' => 1106,
            'encrypt_name' => TRUE
        ),
    );
     
   	public function __construct()
    {
        parent::__construct();// a must for constructor
        $this->load->model('tdatabase_model', 'tdatabase');
        $this->load->library('image_lib'); //load library
    }
    
 	public function delete($imgID)
    {
        $db_images = $this->tdatabase->get_entry_where('id', $imgID, 'slide');
        if (is_array($db_images)) {
            foreach ($db_images as $img_item) {
                $img = $img_item->image;
                $thumb = $img_item->thumbnail;
                if ($img != "" && $thumb != "") {
                    $this->tdatabase->remove_entry('id', $imgID, 'slide');
                    unlink($this->config['img']['upload_path'] . "/" . $img);
                    unlink($this->config['thumb']['new_image'] . "/" . $thumb);
                    unlink($this->config['gallery']['new_image'] . "/" . $thumb);
                }
            }
        }
    }
         
 	public function upload($collID)
    {
   	    $this->load->library('upload', $this->config['img']);
		if ( ! $this->upload->upload("files".$collID)) {
            $data['error'] = $this->upload->display_errors();
            echo join('<br>', $data);
		} else {
            $upload_data = $this->upload->data();
                   
            #create slide image.
            $this->image_lib->clear();
			$this->config['gallery']['source_image'] = $upload_data['full_path'];
            if ($upload_data['image_height'] / 200 > $upload_data['image_width'] / 270) {
                $this->config['gallery']['height'] = 9000;
                $this->config['gallery']['width'] = 270;
            } else {
                $this->config['gallery']['height'] = 200;
                $this->config['gallery']['width'] = 9000;
            }
            $this->config['gallery']['maintain_ratio'] = TRUE;
            $this->image_lib->initialize($this->config['gallery']); 
            $this->image_lib->resize(); //do whatever specified in config     
            
            $this->image_lib->clear();
            $this->config['gallery']['source_image'] = $upload_data['file_path'] . "/slide/" . $upload_data['file_name'];
            $this->config['gallery']['height'] = 200;
            $this->config['gallery']['width'] = 270;
            $this->config['gallery']['maintain_ratio'] = FALSE;
            $this->image_lib->initialize($this->config['gallery']); 
            $this->image_lib->crop(); #do whatever specified in config
            
            $data['id'] = 1;
            $data['img'] = $upload_data['file_name'];
            $this->tdatabase->update_entry($data, 'aboutus');
		}
    }
    
    public function get($collID)
    {
        $images = $this->tdatabase->get_entry_where('collID', $collID, 'slide');
        return $images;
    }
    
    public function getHtmlBox($collID)
    {
        $images = $this->tdatabase->get_entry('slide');
        $imagesHtml = '';
        if (is_array($images)) {
            foreach ($images as $img) {
                $data['box'] = $img;
                $imagesHtml .= $this->smarty->fetch('admin/slide/imgBox.tpl', $data);
            }
        }
        return $imagesHtml;
    }

}
